package web.eshop.order;

import application.MySession;
import domain.shop.DeliveryMethod;
import domain.shop.PayMethod;
import org.apache.wicket.markup.html.form.*;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.LoadableDetachableModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import service.facade.customer.ShopCustomerService;

import java.util.List;

/**
 * Display page, where customer select pay method and delivery method
 */
public final class OrderPanel2 extends Panel {

    @SpringBean
    private ShopCustomerService customerService;
    @SpringBean
    private MySession mySession;


    private Form mainForm = new Form("mainForm");
    private RadioChoice deliveryMethodRadioChoice;
    private RadioChoice payMethodRadioChoice;


    OrderPanel2(String id, final OrderPage parent) {
        super(id);


        IModel<List<DeliveryMethod>> deliveryMethodsModel = new LoadableDetachableModel() {
            @Override
            protected Object load() {
                return customerService.findAllDeliveryMethods();
            }

        };

//            sList.add(i + ") " + dm.getName() + " (cena: " + dm.getPrice() + ")");

        IModel<List<PayMethod>> paymentMethodsModel = new LoadableDetachableModel() {
            @Override
            protected Object load() {
                return customerService.findAllPayMethods();
            }
        };

        initDeliveryMethod(deliveryMethodsModel);
        initPayMethod(paymentMethodsModel);
        mainForm.add(new Button("finishOrder") {

            @Override
            public void onSubmit() {
                OrderPanel3 finalOrderPanel = new OrderPanel3("content", parent);
                parent.replacePanel(finalOrderPanel);

            }
        });
        add(mainForm);
    }

    private void initDeliveryMethod(IModel<List<DeliveryMethod>> model) {
        IChoiceRenderer iChoiceRenderer = new ChoiceRenderer<DeliveryMethod>() {
            @Override
            public Object getDisplayValue(DeliveryMethod object) {
                return object.getName();
            }

            @Override
            public String getIdValue(DeliveryMethod object, int index) {
                return object.getId().toString();
            }
        };
        deliveryMethodRadioChoice = new RadioChoice<DeliveryMethod>("delivery", new PropertyModel(mySession, "deliveryMethod"),
                model, iChoiceRenderer);

        deliveryMethodRadioChoice.setRequired(true);
        mainForm.add(deliveryMethodRadioChoice);
        add(mainForm);
    }

    private void initPayMethod(IModel<List<PayMethod>> model) {
        IChoiceRenderer iChoiceRenderer = new ChoiceRenderer<PayMethod>() {
            @Override
            public Object getDisplayValue(PayMethod object) {
                return object.getName();
            }

            @Override
            public String getIdValue(PayMethod object, int index) {
                return object.getId().toString();
            }
        };
        payMethodRadioChoice = new RadioChoice<PayMethod>("pay", new PropertyModel(mySession, "payMethod"), model, iChoiceRenderer);

//        rc = rc.setPrefix("<tr><td>");
//        rc = rc.setSuffix("</td></tr><br />");
        payMethodRadioChoice.setRequired(true);
        mainForm.add(payMethodRadioChoice);
    }
}




